package com.ruoyi.web.controller.app;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.ruoyi.app.domain.AppVipCard;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.service.IAppUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.OrderNumUtils;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.app.domain.vo.AppVipCardVo;
import com.ruoyi.app.domain.bo.AppVipCardBo;
import com.ruoyi.app.service.IAppVipCardService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 广告卡列表
 *
 * @author ruoyi
 * @date 2025-07-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/vipCard")
public class AppVipCardController extends BaseController {

    private final IAppUserService appUserService;
    private final IAppVipCardService iAppVipCardService;

    /**
     * 查询广告卡列表列表
     */
    @SaCheckPermission("system:vipCard:list")
    @GetMapping("/list")
    public TableDataInfo<AppVipCardVo> list(AppVipCardBo bo, PageQuery pageQuery) {
        return iAppVipCardService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出广告卡列表列表
     */
    @SaCheckPermission("system:vipCard:export")
    @Log(title = "广告卡列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppVipCardBo bo, HttpServletResponse response) {
        List<AppVipCardVo> list = iAppVipCardService.queryList(bo);
        ExcelUtil.exportExcel(list, "广告卡列表", AppVipCardVo.class, response);
    }

    /**
     * 获取广告卡列表详细信息
     *
     * @param cardId 主键
     */
    @SaCheckPermission("system:vipCard:query")
    @GetMapping("/{cardId}")
    public R<AppVipCardVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long cardId) {
        return R.ok(iAppVipCardService.queryById(cardId));
    }

    /**
     * 新增广告卡列表
     */
    @SaCheckPermission("system:vipCard:add")
    @Log(title = "广告卡列表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppVipCardBo bo) {
        AppUserVo userVo = appUserService.queryByPhone(bo.getCardPhone());
        if (Objects.isNull(userVo)) {
            return R.fail("赠送用户不存在！");
        }
        AppVipCardBo appVipCardBo = new AppVipCardBo();
        appVipCardBo.setCardName(bo.getCardName());
        List<AppVipCardVo> cardVos = iAppVipCardService.queryList(appVipCardBo);
        if (!cardVos.isEmpty()) {
            return R.fail("卡片名称不能重复！");
        }
        ArrayList<AppVipCard> appVipCards = new ArrayList<>();
        for (int i = 0; i < bo.getCardMax(); i++) {
            AppVipCard card = new AppVipCard();
            card.setCardName(bo.getCardName());
            card.setCardDay(bo.getCardDay());
            card.setCardUser(userVo.getId());
            card.setCardNumber(UUID.randomUUID().toString());
            card.setCardEndTime(bo.getCardEndTime());
            card.setStaus(Constants.SUCCESS);
            card.setRemark(bo.getRemark());
            appVipCards.add(card);
        }
        if (!appVipCards.isEmpty()) {
            iAppVipCardService.inserts(appVipCards);
        }
        return R.ok();
    }

    /**
     * 修改广告卡列表
     */
    @SaCheckPermission("system:vipCard:edit")
    @Log(title = "广告卡列表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppVipCardBo bo) {
        return toAjax(iAppVipCardService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除广告卡列表
     *
     * @param cardIds 主键串
     */
    @SaCheckPermission("system:vipCard:remove")
    @Log(title = "广告卡列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cardIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] cardIds) {
        return toAjax(iAppVipCardService.deleteWithValidByIds(Arrays.asList(cardIds), true) ? 1 : 0);
    }
}
